package com.rudolfheszele.smsselector.model;

public class ActionFactory
{
	public enum ActionId implements Id
	{
		MOVE_TO_FOLDER("Move to folder", 0),
		DELETE_MESSAGE("Delete message", 1);
		
		@Override
		public int getIntValue()
		{
			return mIntValue;
		}
		
		@Override
		public String getName()
		{
			return mRuleName;
		}
		
		private ActionId(String ruleName, int intValue)
		{
			mRuleName = ruleName;
			mIntValue = intValue;
		}
		
		private int mIntValue;
		private String mRuleName;
	}
	
	public static SmsSelectorRuleSet.Action createAction(ActionId actionId, String additionalInfo)
	{
		SmsSelectorRuleSet.Action action = null;
		
		switch(actionId)
		{
			case MOVE_TO_FOLDER:
				action = new MoveToFolderAction(additionalInfo);
				break;
			case DELETE_MESSAGE:
				action = new DeleteMessageAction();
				break;
		}
		if(action != null)
			action.setId(actionId);
		
		return action;
	}
	
	public static ActionId getActionId(int actionId)
	{
		ActionId[] actionIds = ActionId.values();
		
		for(int i = 0; i < actionIds.length; i++)
		{
			if(actionId == actionIds[i].getIntValue())
				return actionIds[i];
		}
		
		return null;
	}
	
	public static ActionId getActionId(String actionName)
	{
		ActionId[] actionIds = ActionId.values();
		
		for(int i = 0; i < actionIds.length; i++)
		{
			if(actionName.equals(actionIds[i].getName()))
				return actionIds[i];
		}
		
		return null;
	}
	
	public static int getNumberOfActions()
	{
		return ActionId.values().length;
	}
}

class MoveToFolderAction implements SmsSelectorRuleSet.Action
{
	public MoveToFolderAction(String folder)
	{
		mFolder = folder;
	}

	@Override
	public Object doAction(Object object)
	{
		SmsSelectorSmsMessage message = (SmsSelectorSmsMessage)object;
		
		message.setLogicalFolder(mFolder);
		
		return message;
	}
	
	@Override
	public String getAdditionalInfo()
	{
		return mFolder;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	private Id mId;
	private String mFolder;
}

class DeleteMessageAction implements SmsSelectorRuleSet.Action
{

	@Override
	public Object doAction(Object object)
	{
		SmsSelectorSmsMessage message = (SmsSelectorSmsMessage)object;
		
		message.deleteMessage();
		
		return message;
	}
	
	@Override
	public String getAdditionalInfo()
	{
		return null;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	private Id mId;
}
